package com.tencent.qcloud.tim.demo.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBHelper {
    // 数据库连接配置
    private static String URL = "jdbc:mysql://mysql2.sqlpub.com:3307/kislasureos_account?useSSL=false&useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true&useSSL=false";

    private static final String USER = "kislasureos";
    private static final String PASSWORD = "n5L0y9ZCDvnpOAXA";
    private static Connection connection;

    //private static String URL = "jdbc:mysql://192.168.10.11:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true&useSSL=false" ;
    //private static final String USER = "jyq";
    // private static final String PASSWORD = "jyq422";
    public static Connection getConnection() throws SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            return DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new SQLException("MySQL JDBC driver not found");
        }
    }

    public static boolean findUserExist(String phone) throws SQLException {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        connection=getConnection();
        String query = "SELECT * FROM users WHERE phone_number = ?";
        stmt = connection.prepareStatement(query);
        stmt.setString(1, phone);
        rs = stmt.executeQuery();
        if (rs.next()) {
            return true;
        } else {
            return false;
        }

    }

    public static int checkUserAccount(String phone, String passwprd) {

        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            connection = getConnection();
            System.out.println("连上了——");
            String query = "SELECT * FROM users WHERE phone_number = ? and password=?";
            stmt = connection.prepareStatement(query);
            stmt.setString(1, phone);
            stmt.setString(2, passwprd);
            rs = stmt.executeQuery();

            if (rs.next()) {
                // 用户存在
                String password = rs.getString("password");
                System.out.println("用户密码: " + password);
                return 1;
            } else {
                boolean flag = findUserExist(phone);
                if (flag) {
                    //账户存在但是密码错误
                    return -1;
                } else {
                    //账户不存在
                    return -2;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        } finally {
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (connection != null) connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static int registerUser(String phone, String password) {
        PreparedStatement stmt = null;
        try {
            // 检查用户是否已存在
            if (findUserExist(phone)) {
                // 用户已存在
                return -1;
            }
            // 如果用户不存在，进行注册操作
            connection = getConnection();
            String query = "INSERT INTO users (phone_number, password) VALUES (?, ?)";
            stmt = connection.prepareStatement(query);
            stmt.setString(1, phone);
            stmt.setString(2, password);

            int result = stmt.executeUpdate();
            if (result > 0) {
                // 注册成功
                return 1;
            } else {
                // 注册失败
                return 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return -2;  // 发生错误
        } finally {
            try {
                if (stmt != null) stmt.close();
                if (connection != null) connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
